package com.zhoug.player3.exo;

import android.content.Context;


import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.DefaultHttpDataSource;
import androidx.media3.datasource.TransferListener;
import androidx.media3.exoplayer.DefaultRenderersFactory;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.RenderersFactory;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.extractor.DefaultExtractorsFactory;


/**
 * 用于创建ExoPlayer
 *
 * @Author: zhoug
 * @Date: 2023/1/6
 * @Description:
 */
@UnstableApi
class ExoPlayerFactory {
    private static final String TAG = ">>>ExoPlayerFactory";
    private static final boolean DEBUG = BuildConfig.DEBUG;

    public static ExoPlayer createExoPlayer(Context context) {
//        return new ExoPlayer.Builder(context,
//                getDefaultRenderersFactory(context),
//                new DefaultMediaSourceFactory(getDefaultHttpDataSourceFactory())
//        ).build();
//        return new ExoPlayer.Builder(context).build();
        DefaultExtractorsFactory defaultExtractorsFactory = new DefaultExtractorsFactory();
        //无效,在创建MediaSource设置
//        defaultExtractorsFactory.setTsExtractorFlags(DefaultTsPayloadReaderFactory.FLAG_DETECT_ACCESS_UNITS);
        return new ExoPlayer.Builder(context,
                getDefaultRenderersFactory(context),
                new DefaultMediaSourceFactory(context, defaultExtractorsFactory)

        ).build();

    }

    public static ExoPlayer createExoPlayer1(Context context) {
        ExoPlayer.Builder builder = new ExoPlayer.Builder(context);


        return builder.build();
    }

    /**
     * 默认渲染器工厂
     *
     * @param context
     * @return
     */
    public static RenderersFactory getDefaultRenderersFactory(Context context) {
        DefaultRenderersFactory defaultRenderersFactory = new DefaultRenderersFactory(context)
                //设置如果解码器初始化失败，是否启用回退到低优先级解码器。
                // 这可能导致使用比主解码器效率更低或更慢的解码器
                .setEnableDecoderFallback(true)
                //设置视频渲染器可以尝试无缝加入正在进行的播放的最大持续时间
                .setAllowedVideoJoiningTimeMs(10000)
                //允许使用扩展渲染器。扩展渲染器索引在相同类型的核心渲染器之后。因此，偏好第一个合适的渲染器的｛@link TrackSelector｝将倾向于使用核心渲染器而不是扩展渲染器，因为两者都能够播放给定的曲目
                .setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);


        return defaultRenderersFactory;
    }

    /**
     * 默认的http DataSource工厂
     *
     * @return
     */
    public static DataSource.Factory getDefaultHttpDataSourceFactory() {
        DefaultHttpDataSource.Factory dataSourceFactory = new DefaultHttpDataSource.Factory();
        dataSourceFactory.setAllowCrossProtocolRedirects(true);//跨协议重定向
        //设置当我们对POST请求进行HTTP 302重定向时，是否应保留POST方法和主体
        dataSourceFactory.setKeepPostFor302Redirects(true);
        dataSourceFactory.setTransferListener(new TransferListener() {
            /**
             * 在初始化传输时调用
             * @param source The source performing the transfer.
             * @param dataSpec Describes the data for which the transfer is initialized.
             * @param isNetwork Whether the data is transferred through a network.
             */
            @Override
            public void onTransferInitializing(DataSource source, DataSpec dataSpec, boolean isNetwork) {
                /*if (DEBUG) {
                    Log.d(TAG, "初始化传输 onTransferInitializing:"
                            + "isNetwork:" + isNetwork
                    );
                }*/

            }

            /**
             * 在传输开始时调用
             * @param source The source performing the transfer.
             * @param dataSpec Describes the data being transferred.
             * @param isNetwork Whether the data is transferred through a network.
             */
            @Override
            public void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork) {
               /* if (DEBUG) {
                    Log.d(TAG, "传输开始 onTransferStart:"
                            + "isNetwork:" + isNetwork
                            + ",position:" + dataSpec.position
                            + ",length:" + dataSpec.length
                            + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                            + ",key:" + dataSpec.key
                            + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                    );
                }*/
            }

            @Override
            public void onBytesTransferred(DataSource source, DataSpec dataSpec, boolean isNetwork, int bytesTransferred) {
               /* if (DEBUG) {
                    Log.d(TAG, "传输过程 onBytesTransferred:"
                            + "isNetwork:" + isNetwork
                            + ",bytesTransferred:" + bytesTransferred
                            + ",position:" + dataSpec.position
                            + ",length:" + dataSpec.length
                            + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                            + ",key:" + dataSpec.key
                            + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                    );
                }*/

            }

            @Override
            public void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork) {
                /*if (DEBUG) {
                    Log.d(TAG, "传输结束 onTransferEnd:"
                            + "isNetwork:" + isNetwork
                            + ",position:" + dataSpec.position
                            + ",length:" + dataSpec.length
                            + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                            + ",key:" + dataSpec.key
                            + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                    );
                }*/


            }
        });

        return dataSourceFactory;
    }

}
